package com.jinlejia.bbs.ui.mine;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.mine.AttentionAdapter;
import com.jinlejia.bbs.bean.mine.AttentionBean;
import com.jinlejia.bbs.databinding.BbsActivityAttentionBinding;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.AttentionLogic;
import com.jinlejia.bbs.widget.recycler.PullInterface;

import java.util.ArrayList;

/**
 * 我的关注页面
 */
public class BBSAttentionActivity extends AppCompatActivity implements AttentionLogic.LogicCallBack, PullInterface.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private BbsActivityAttentionBinding binding;

    private AttentionLogic logic;
    private ArrayList<AttentionBean> datas;
    private String pin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.bbs_activity_attention);
        binding.setCommonEvent(CommonEvent.obtain());
        datas = new ArrayList<>();
        logic = new AttentionLogic(this,datas,this);
        pin = getIntent().getStringExtra("pin");
        initView();
    }
    private AttentionAdapter adapter;
    private void initView() {
        adapter = new AttentionAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAutoLoadMoreListener(this);
        binding.swipeRefreshLayout.setOnRefreshListener(this);
        binding.swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.swipeRefreshLayout.setRefreshing(true);
                logic.refresh(pin);
            }
        });
    }


    @Override
    public void onResult(boolean hasNextPage) {
        adapter.initData(datas);
        if (!hasNextPage){
            binding.recyclerView.setAutoLoadMoreListener(null);
        }else {
            binding.recyclerView.setAutoLoadMoreListener(this);
        }
        binding.recyclerView.getAdapter().notifyDataSetChanged();
        binding.swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onLoadMore() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.load(pin);
    }

    @Override
    public void onRefresh() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.refresh(pin);
    }
}
